package com.example.experiment4.service;


import com.example.experiment4.domain.Person;
import com.example.experiment4.utils.JudgmentInformation;
import com.example.experiment4.utils.ReadImgToOCR;
import com.tencentcloudapi.ocr.v20181119.models.TextDetection;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class ReadImgGetData {

    @Resource
    ReadImgToOCR ocr;

    @Resource
    JudgmentInformation information;

    public List<Person> personList (String filePath){
        List<Person> personList  = new ArrayList<>();
        Person person = new Person();
        TextDetection[] textDetections = ocr.toOCR(filePath);
//        System.out.println(textDetections[textDetections.length-1].getDetectedText());
        for (int i = 0; i <textDetections.length ; i++) {
            String res = textDetections[i].getDetectedText();
//            System.out.println(res);
            if(res.contains(";")){
                res= res.replaceAll(";","");
//                System.out.println(res);
            }
//            System.out.println(res);


            if(information.isAddress(res)){
                if(person.getAddress()!=null){

                    personList.add(person);
                    person=new Person();
                    person.setAddress(res);
                    continue;
                }
                person.setAddress(res);
                continue;
            }

            if(information.isName(res)){
                if(person.getName()!=null){

                    personList.add(person);
                    person=new Person();
                    person.setName(res);
                    continue;
                }
                person.setName(res);

                continue;
            }

            if (information.isPhone(res)){
                if(person.getPhone()!=null){

                    personList.add(person);
                    person=new Person();
                    person.setPhone(res);
                    continue;
                }
                person.setPhone(res);
                continue;
            }

        }
        return personList;
//        for (int i = 0; i <personList.size() ; i++) {
//            System.out.println(personList.get(i).toString());
//        }
//        System.out.println();
    }
}
